// SQL IN生成器模块
class SQLGenerator {
    static generate(input, options = {}) {
        const {
            column = 'id',
            quoteType = 'single',
            removeDuplicates = false,
            sortValues = false
        } = options;

        if (!input || !input.trim()) {
            return '';
        }

        try {
            // 分割输入并清理
            let values = input.split('\n')
                .map(line => line.trim())
                .filter(line => line !== '');

            if (values.length === 0) {
                return '';
            }

            // 去重
            if (removeDuplicates) {
                values = [...new Set(values)];
            }

            // 排序
            if (sortValues) {
                values.sort((a, b) => {
                    // 尝试数字排序
                    const numA = parseFloat(a);
                    const numB = parseFloat(b);
                    if (!isNaN(numA) && !isNaN(numB)) {
                        return numA - numB;
                    }
                    // 字符串排序
                    return a.localeCompare(b);
                });
            }

            // 添加引号
            let quote = '';
            switch (quoteType) {
                case 'single':
                    quote = "'";
                    break;
                case 'double':
                    quote = '"';
                    break;
                case 'none':
                default:
                    quote = '';
                    break;
            }

            // 转义引号
            const quotedValues = values.map(value => {
                if (quote) {
                    // 转义相同类型的引号
                    const escapedValue = value.replace(new RegExp(quote, 'g'), quote + quote);
                    return `${quote}${escapedValue}${quote}`;
                }
                return value;
            });

            // 生成SQL
            const inClause = quotedValues.join(', ');
            const sql = `SELECT * FROM table_name WHERE ${column} IN (${inClause});`;

            return sql;

        } catch (error) {
            console.error('SQL生成错误:', error);
            return '';
        }
    }

    // 生成其他类型的SQL语句
    static generateDelete(input, options = {}) {
        const {
            tableName = 'table_name',
            column = 'id',
            quoteType = 'single'
        } = options;

        const inClause = this.generateInClause(input, { column, quoteType });
        if (!inClause) return '';

        return `DELETE FROM ${tableName} WHERE ${inClause};`;
    }

    static generateUpdate(input, options = {}) {
        const {
            tableName = 'table_name',
            column = 'id',
            setClause = 'status = 1',
            quoteType = 'single'
        } = options;

        const inClause = this.generateInClause(input, { column, quoteType });
        if (!inClause) return '';

        return `UPDATE ${tableName} SET ${setClause} WHERE ${inClause};`;
    }

    static generateInClause(input, options = {}) {
        const { column = 'id', quoteType = 'single' } = options;
        
        if (!input || !input.trim()) return '';

        const values = input.split('\n')
            .map(line => line.trim())
            .filter(line => line !== '');

        if (values.length === 0) return '';

        let quote = '';
        switch (quoteType) {
            case 'single': quote = "'"; break;
            case 'double': quote = '"'; break;
            default: quote = '';
        }

        const quotedValues = values.map(value => {
            if (quote) {
                const escapedValue = value.replace(new RegExp(quote, 'g'), quote + quote);
                return `${quote}${escapedValue}${quote}`;
            }
            return value;
        });

        return `${column} IN (${quotedValues.join(', ')})`;
    }

    // 解析现有的IN语句
    static parseInClause(sql) {
        try {
            const inRegex = /(\w+)\s+IN\s*\(([^)]+)\)/i;
            const match = sql.match(inRegex);
            
            if (!match) {
                return { success: false, error: '未找到有效的IN语句' };
            }

            const column = match[1];
            const valuesStr = match[2];
            
            // 解析值
            const values = [];
            let current = '';
            let inQuote = false;
            let quoteChar = '';
            
            for (let i = 0; i < valuesStr.length; i++) {
                const char = valuesStr[i];
                
                if (!inQuote && (char === "'" || char === '"')) {
                    inQuote = true;
                    quoteChar = char;
                } else if (inQuote && char === quoteChar) {
                    // 检查是否是转义引号
                    if (i + 1 < valuesStr.length && valuesStr[i + 1] === quoteChar) {
                        current += char;
                        i++; // 跳过下一个引号
                    } else {
                        inQuote = false;
                        quoteChar = '';
                    }
                } else if (!inQuote && char === ',') {
                    values.push(current.trim());
                    current = '';
                } else if (char !== ' ' || inQuote) {
                    current += char;
                }
            }
            
            if (current.trim()) {
                values.push(current.trim());
            }

            return {
                success: true,
                data: {
                    column,
                    values: values.filter(v => v !== ''),
                    quoteType: quoteChar ? (quoteChar === "'" ? 'single' : 'double') : 'none'
                }
            };
        } catch (error) {
            return { success: false, error: error.message };
        }
    }
}